<template>
  <el-dialog title="新增员工" :visible.sync="dialogShow" @close="closeDialog">
    <!--绘制表单-->
    <el-form v-if="dialogShow" ref="form" :model="formData" :rules="formRules" label-width="140px">
      <el-form-item label="姓名" prop="username" size="normal">
        <el-input v-model="formData.username" style="width:320px" placeholder="请输入姓名"/>
      </el-form-item>
      <el-form-item label="手机" prop="mobile" size="normal">
        <el-input v-model="formData.mobile" style="width:320px" placeholder="请输入手机号"/>
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry" size="normal">
        <el-date-picker v-model="formData.timeOfEntry" style="width:320px" placeholder="请选择入职时间"/>
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment" size="normal">
        <el-select v-model="formData.formOfEmployment" style="width:320px" placeholder="请选择聘用形式">
          <el-option v-for="item in hireType" :key="item.id" :label="item.value" :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber" size="normal">
        <el-input v-model="formData.workNumber" style="width:320px" placeholder="请输入工号"/>
      </el-form-item>
      <el-form-item label="部门" prop="departmentName" size="normal">
        <el-input v-model="formData.departmentName" style="width:320px" placeholder="请选择部门" @focus="getDeptList"/>
        <!--部门树-->
        <el-tree
          v-show="treeShow"
          :data="deptList"
          :props="{label:'name'}"
          style="width: 320px"
          @node-click="handleNodeClick"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime" size="normal">
        <el-date-picker v-model="formData.correctionTime" style="width:320px" placeholder="请选择转正时间"/>
      </el-form-item>
      <el-row type="flex" justify="center">
        <el-button size="small" type="primary" @click="submitRole">提交</el-button>
        <el-button size="small" @click="dialogShow = false">重置</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import EmployeeEnum from '@/api/constant/employees'
import { tranListToTreeData } from '@/utils'
import { getDepartmentListApi } from '@/api/department'
import { addEmployeesApi } from '@/api/employees'

export default {
  name: 'AddEmployees',
  data() {
    return {
      dialogShow: false,
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      // 表单规则变量
      formRules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '请选择聘用形式', trigger: 'change' }
        ],
        workNumber: [
          { required: true, message: '请输入工号', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '请选择部门', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '请选择转正时间', trigger: 'blur' }
        ]
      },
      // 聘用形式
      hireType: EmployeeEnum.hireType,
      // 部门数据
      deptList: [],
      // 部门树是否显示
      treeShow: false
    }
  },
  methods: {
    // 新增员工提交
    async submitRole() {
      try {
        await this.$refs.form.validate()
      } catch (error) {
        return console.log(error)
      }
      await addEmployeesApi(this.formData)
      this.$message.success('新增成功')
      this.$emit('updateList')
      this.dialogShow = false
    },
    // 弹窗关闭事件
    closeDialog() {
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      this.$refs.form.clearValidate()
    },
    // 获取部门列表
    async getDeptList() {
      const { depts } = await getDepartmentListApi()
      this.deptList = tranListToTreeData(depts, '')
      this.treeShow = true
    },
    // 部门树点击事件
    handleNodeClick(data) {
      this.formData.departmentName = data.name
      this.treeShow = false
    }
  }
}
</script>

<style scoped></style>
